﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using Portal.Game.DAL;
using Portal.Core.DAL;
using System.ComponentModel;
using System.Collections;
using Portal.User.Security;
using System.Web.Caching;
using Xmobile.Users;

namespace Portal.BO.Editoral.Category
{
	public static class CategoryHelper
	{

		public static string LoadAllCat(DropDownList cb_Cate, ListBox lb_Cate, CheckBoxList cbl_Cate)
		{
			string strResult = "";
			using (Portal.Core.DAL.MainDB objDB = new Portal.Core.DAL.MainDB())
			{
                DataTable parentRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where (Cat_ParentID =0 OR Cat_ParentID is null) ", " Order By Cat_Order ");

				if (cb_Cate != null)
					cb_Cate.Items.Add(new ListItem("-----Chọn tất cả -----", "0"));
				if (lb_Cate != null)
					lb_Cate.Items.Add(new ListItem("-----Chọn chuyên mục khác -----", "0"));

				DataTable childRows;
				DataTable childLevel3Rows;
				foreach (DataRow aRow in parentRows.Rows)
				{
                    childRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + aRow["Cat_ID"] + " ", " Order By Cat_Order ");

					if (cb_Cate != null)
						cb_Cate.Items.Add(new ListItem("" + aRow["Cat_Name"].ToString().ToUpper() + "", aRow["Cat_ID"] + ""));
					if (lb_Cate != null)
						lb_Cate.Items.Add(new ListItem("" + aRow["Cat_Name"].ToString().ToUpper() + "", aRow["Cat_ID"] + ""));
					if (cbl_Cate != null)
						cbl_Cate.Items.Add(new ListItem("" + aRow["Cat_Name"].ToString().ToUpper() + "", aRow["Cat_ID"] + ""));

					strResult += aRow["Cat_ID"] + ",";
					foreach (DataRow RowChild in childRows.Rows)
					{
						if (cb_Cate != null)
							cb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
						if (lb_Cate != null)
							lb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
						if (cbl_Cate != null)
							cbl_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], RowChild["Cat_ID"] + ""));

						strResult += RowChild["Cat_ID"] + ",";

						// Xu ly o tang 3
                        childLevel3Rows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + RowChild["Cat_ID"] + " ", " Order By Cat_Order ");
						if (childLevel3Rows != null)
						{
							foreach (DataRow RowChildLevel3Category in childLevel3Rows.Rows)
							{
								if (cb_Cate != null)
									cb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
								if (lb_Cate != null)
									lb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
								if (cbl_Cate != null)
									cbl_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], RowChildLevel3Category["Cat_ID"] + ""));

								strResult += RowChildLevel3Category["Cat_ID"] + ",";
							}
						}
					}

				}
				return strResult;
			}
		}

        public static string LoadAllCatbyChannelID(DropDownList cb_Cate, ListBox lb_Cate, CheckBoxList cbl_Cate, string channelID)
        {
            string strResult = "";
            using (Portal.Core.DAL.MainDB objDB = new Portal.Core.DAL.MainDB())
            {
                DataTable parentRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where (Cat_ParentID =0 OR Cat_ParentID is null) and Channel_ID = "+ channelID, " Order By Cat_Order ");

                if (cb_Cate != null)
                    cb_Cate.Items.Add(new ListItem("-----Chuyên mục cha-----", "0"));
                if (lb_Cate != null)
                    lb_Cate.Items.Add(new ListItem("-----Chọn chuyên mục khác -----", "0"));

                DataTable childRows;
                DataTable childLevel3Rows;
                foreach (DataRow aRow in parentRows.Rows)
                {
                    childRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + aRow["Cat_ID"] + " ", " Order By Cat_Order ");

                    if (cb_Cate != null)
                        cb_Cate.Items.Add(new ListItem("" + aRow["Cat_Name"].ToString().ToUpper() + "", aRow["Cat_ID"] + ""));
                    if (lb_Cate != null)
                        lb_Cate.Items.Add(new ListItem("" + aRow["Cat_Name"].ToString().ToUpper() + "", aRow["Cat_ID"] + ""));
                    if (cbl_Cate != null)
                        cbl_Cate.Items.Add(new ListItem("" + aRow["Cat_Name"].ToString().ToUpper() + "", aRow["Cat_ID"] + ""));

                    strResult += aRow["Cat_ID"] + ",";
                    foreach (DataRow RowChild in childRows.Rows)
                    {
                        if (cb_Cate != null)
                            cb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
                        if (lb_Cate != null)
                            lb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
                        if (cbl_Cate != null)
                            cbl_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], RowChild["Cat_ID"] + ""));

                        strResult += RowChild["Cat_ID"] + ",";

                        // Xu ly o tang 3
                        childLevel3Rows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + RowChild["Cat_ID"] + " ", " Order By Cat_Order ");
                        if (childLevel3Rows != null)
                        {
                            foreach (DataRow RowChildLevel3Category in childLevel3Rows.Rows)
                            {
                                if (cb_Cate != null)
                                    cb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
                                if (lb_Cate != null)
                                    lb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
                                if (cbl_Cate != null)
                                    cbl_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], RowChildLevel3Category["Cat_ID"] + ""));

                                strResult += RowChildLevel3Category["Cat_ID"] + ",";
                            }
                        }
                    }

                }
                return strResult;
            }
        }

		private static string LoadCategoryByPermission(DropDownList cb_Cate, ListBox lb_Cate, CheckBoxList cbl_Cate)
		{
			string strResult = "";
			MainSecurity objSec = new MainSecurity();
			Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
			DataTable objParentCat = objSec.getParentCategoryAssigned(HttpContext.Current.User.Identity.Name, Portal.API.Config.CurrentChannel);
            
			if (cb_Cate != null)
				cb_Cate.Items.Add(new ListItem("-----Chọn tất cả -----", "0"));
			if (lb_Cate != null)
				lb_Cate.Items.Add(new ListItem("-----Chọn chuyên mục khác -----", "0"));

			DataTable childRows = new DataTable();
			DataTable childLevel3Rows = new DataTable();
			using (Portal.Core.DAL.MainDB objDB = new Portal.Core.DAL.MainDB())
			{
				foreach (DataRow objrow in objParentCat.Rows)
				{
                    childRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + objrow["Cat_ID"] + " ", " Order By Cat_Order ");
					strResult += objrow["Cat_ID"] + ",";

					if (cb_Cate != null)
						cb_Cate.Items.Add(new ListItem("" + objrow["Cat_Name"].ToString().ToUpper() + "", objrow["Cat_ID"] + ""));
					if (lb_Cate != null)
						lb_Cate.Items.Add(new ListItem("" + objrow["Cat_Name"].ToString().ToUpper() + "", objrow["Cat_ID"] + ""));
					if (cbl_Cate != null)
						cbl_Cate.Items.Add(new ListItem("" + objrow["Cat_Name"].ToString().ToUpper() + "", objrow["Cat_ID"] + ""));


					foreach (DataRow RowChild in childRows.Rows)
					{
						if (cb_Cate != null)
							cb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
						if (lb_Cate != null)
							lb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
						if (cbl_Cate != null)
							cbl_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], RowChild["Cat_ID"] + ""));

						strResult += RowChild["Cat_ID"] + ",";

						// Xu ly o tang 3
                        childLevel3Rows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + RowChild["Cat_ID"] + " ", " Order By Cat_Order ");
						if (childLevel3Rows.Rows.Count > 0)
						{
							foreach (DataRow RowChildLevel3Category in childLevel3Rows.Rows)
							{
								if (cb_Cate != null)
									cb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
								if (lb_Cate != null)
									lb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
								if (cbl_Cate != null)
									cbl_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], RowChildLevel3Category["Cat_ID"] + ""));

								strResult += RowChildLevel3Category["Cat_ID"] + ",";
							}
						}
					}
				}
			}

			return strResult;
		}
        
        /* tiennv 18 - 05 - 2010 : */

        private static string CafeF_LoadCategoryByPermission(DropDownList cb_Cate, ListBox lb_Cate, CheckBoxList cbl_Cate)
        {
            string strResult = "";
            MainSecurity objSec = new MainSecurity();
            Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
            DataTable objParentCat = objSec.getParentCategoryAssigned(HttpContext.Current.User.Identity.Name, Portal.API.Config.CurrentChannel);
            DataTable dtPermissionCat = new DataTable();
                        
            if (cb_Cate != null)
                cb_Cate.Items.Add(new ListItem("-----Chọn tất cả -----", "0"));
            if (lb_Cate != null)
                lb_Cate.Items.Add(new ListItem("-----Chọn chuyên mục khác -----", "0"));

            DataTable childRows = new DataTable();
            DataTable childLevel3Rows = new DataTable();
            using (Portal.Core.DAL.MainDB objDB = new Portal.Core.DAL.MainDB())
            {
                foreach (DataRow objrow in objParentCat.Rows)
                {

                    dtPermissionCat = objSec.CafeF_Select_Permission_by_Cat(HttpContext.Current.User.Identity.Name, Convert.ToInt32(objrow["cat_id"]));

                    if (dtPermissionCat != null && dtPermissionCat.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtPermissionCat.Rows.Count; i++)
                        {

                            if (Convert.ToInt32(dtPermissionCat.Rows[i]["Permission_ID"]) == PermissionConst.Xuat_Ban_Bai)
                            {
                                childRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + objrow["Cat_ID"] + " ", " Order By Cat_Order ");
                                strResult += objrow["Cat_ID"] + ",";

                                if (cb_Cate != null)
                                    cb_Cate.Items.Add(new ListItem("" + objrow["Cat_Name"].ToString().ToUpper() + "", objrow["Cat_ID"] + ""));
                                if (lb_Cate != null)
                                    lb_Cate.Items.Add(new ListItem("" + objrow["Cat_Name"].ToString().ToUpper() + "", objrow["Cat_ID"] + ""));
                                if (cbl_Cate != null)
                                    cbl_Cate.Items.Add(new ListItem("" + objrow["Cat_Name"].ToString().ToUpper() + "", objrow["Cat_ID"] + ""));


                                foreach (DataRow RowChild in childRows.Rows)
                                {
                                    if (cb_Cate != null)
                                        cb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
                                    if (lb_Cate != null)
                                        lb_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], Convert.ToString(RowChild["Cat_ID"])));
                                    if (cbl_Cate != null)
                                        cbl_Cate.Items.Add(new ListItem("→" + RowChild["Cat_Name"], RowChild["Cat_ID"] + ""));

                                    strResult += RowChild["Cat_ID"] + ",";

                                    // Xu ly o tang 3
                                    childLevel3Rows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + RowChild["Cat_ID"] + " ", " Order By Cat_Order ");
                                    if (childLevel3Rows.Rows.Count > 0)
                                    {
                                        foreach (DataRow RowChildLevel3Category in childLevel3Rows.Rows)
                                        {
                                            if (cb_Cate != null)
                                                cb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
                                            if (lb_Cate != null)
                                                lb_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], Convert.ToString(RowChildLevel3Category["Cat_ID"])));
                                            if (cbl_Cate != null)
                                                cbl_Cate.Items.Add(new ListItem("→→" + RowChildLevel3Category["Cat_Name"], RowChildLevel3Category["Cat_ID"] + ""));

                                            strResult += RowChildLevel3Category["Cat_ID"] + ",";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return strResult;
        }

        private static string CafeF_LoadCategoryByPermission(string user)
        {
            string strResult = "";
            MainSecurity objSec = new MainSecurity();
            Role objrole = objSec.GetRole(user);
            DataTable objParentCat = objSec.getParentCategoryAssigned(user, Portal.API.Config.CurrentChannel);
            DataTable dtPermissionCat = new DataTable();
            
            DataTable childRows = new DataTable();
            DataTable childLevel3Rows = new DataTable();
            using (Portal.Core.DAL.MainDB objDB = new Portal.Core.DAL.MainDB())
            {
                foreach (DataRow objrow in objParentCat.Rows)
                {

                    dtPermissionCat = objSec.CafeF_Select_Permission_by_Cat(user, Convert.ToInt32(objrow["cat_id"]));

                    if (dtPermissionCat != null && dtPermissionCat.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtPermissionCat.Rows.Count; i++)
                        {

                            if (Convert.ToInt32(dtPermissionCat.Rows[i]["Permission_ID"]) == PermissionConst.Xuat_Ban_Bai)
                            {
                                childRows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + objrow["Cat_ID"] + " ", " Order By Cat_Order ");
                                strResult += objrow["Cat_ID"] + ",";                             


                                foreach (DataRow RowChild in childRows.Rows)
                                {
                                    

                                    strResult += RowChild["Cat_ID"] + ",";

                                    // Xu ly o tang 3
                                    childLevel3Rows = objDB.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID=" + RowChild["Cat_ID"] + " ", " Order By Cat_Order ");
                                    if (childLevel3Rows.Rows.Count > 0)
                                    {
                                        foreach (DataRow RowChildLevel3Category in childLevel3Rows.Rows)
                                        {                                           
                                             strResult += RowChildLevel3Category["Cat_ID"] + ",";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return strResult;
        }

		/// <summary>
		/// Ham thuc hien bind du lieu ve danh muc cho mot dropdown control
		/// </summary>
		/// <param name="cb_Cate">Dropdown can bind</param>
		/// <returns>Tra ve mot chuoi cac id cua cay duoc cach nhau boi dau ","</returns>
		public static string Treebuild(DropDownList cb_Cate)
		{
			cb_Cate.Items.Clear();
			string strResult = "";

            // Doi voi truong hop luu Cache
            string str_item_cache = HttpContext.Current.Cache.Get("Treebuild_" + HttpContext.Current.User.Identity.Name) as string;
            strResult = HttpContext.Current.Cache.Get("Treebuild_strResult" + HttpContext.Current.User.Identity.Name) as string;
            if (!string.IsNullOrEmpty(str_item_cache) && !string.IsNullOrEmpty(strResult))
            {
                #region Da luu cache
                //strResult = HttpContext.Current.Cache.Get("Treebuild_strResult" + HttpContext.Current.User.Identity.Name) as string;

                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
                #endregion
            }
			else
			{
				#region Doi voi truong hop chua luu Cache
				// Doi voi truong hop chua luu cache
				//thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
				MainSecurity objSec = new MainSecurity();
				Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
				if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap)
				{
					strResult = LoadAllCat(cb_Cate, null, null);
				}
				else
				{
					strResult = LoadCategoryByPermission(cb_Cate, null, null);
				}
				if (strResult != "")
					strResult = strResult.Substring(0, strResult.Length - 1);

				str_item_cache = "";
				foreach (ListItem item in cb_Cate.Items)
				{
					str_item_cache += "!" + item.Text + "|" + item.Value;
				}

				if (str_item_cache.Trim() != "")
					str_item_cache = str_item_cache.Substring(1);

				HttpContext.Current.Cache.Insert("Treebuild_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				HttpContext.Current.Cache.Insert("Treebuild_strResult" + HttpContext.Current.User.Identity.Name, strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				#endregion
			}
			return strResult;
		}

        public static string TreebuildbyChannelName(DropDownList cb_Cate)
        {
            cb_Cate.Items.Clear();
            string strResult = "";

            string cacheName = "Treebuild_" + HttpContext.Current.User.Identity.Name + "_" + BO.Common.Const.Channel_Name;
            // Doi voi truong hop luu Cache
            string str_item_cache = HttpContext.Current.Cache.Get(cacheName) as string;
            strResult = HttpContext.Current.Cache.Get(cacheName + "_strResult") as string;
            if (!string.IsNullOrEmpty(str_item_cache) && !string.IsNullOrEmpty(strResult))
            {
                #region Da luu cache
                //strResult = HttpContext.Current.Cache.Get("Treebuild_strResult" + HttpContext.Current.User.Identity.Name) as string;

                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
                #endregion
            }
            else
            {
                #region Doi voi truong hop chua luu Cache
                // Doi voi truong hop chua luu cache
                //thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
                MainSecurity objSec = new MainSecurity();
                Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
                if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap)
                {
                    strResult = LoadAllCat(cb_Cate, null, null);
                }
                else
                {
                    strResult = LoadCategoryByPermission(cb_Cate, null, null);
                }
                if (strResult != "")
                    strResult = strResult.Substring(0, strResult.Length - 1);

                str_item_cache = "";
                foreach (ListItem item in cb_Cate.Items)
                {
                    str_item_cache += "!" + item.Text + "|" + item.Value;
                }

                if (str_item_cache.Trim() != "")
                    str_item_cache = str_item_cache.Substring(1);

                HttpContext.Current.Cache.Insert("Treebuild_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                HttpContext.Current.Cache.Insert("Treebuild_strResult" + HttpContext.Current.User.Identity.Name, strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                #endregion
            }
            return strResult;
        }


        /* tiennv 18 - 05 - 2010 : load vao dropdownlist chuyen muc theo user permission id */

        /// <summary>
        /// Ham thuc hien bind du lieu ve danh muc cho mot dropdown control
        /// </summary>
        /// <param name="cb_Cate">Dropdown can bind</param>
        /// <returns>Tra ve mot chuoi cac id cua cay duoc cach nhau boi dau ","</returns>
        public static string CafeF_Treebuild(DropDownList cb_Cate)
        {
            cb_Cate.Items.Clear();
            string strResult = "";

            // Doi voi truong hop luu Cache
            string str_item_cache = HttpContext.Current.Cache.Get("CafeF_Treebuild_" + HttpContext.Current.User.Identity.Name) as string;
            strResult = HttpContext.Current.Cache.Get("CafeF_Treebuild_strResult" + HttpContext.Current.User.Identity.Name) as string;
            if (!string.IsNullOrEmpty(str_item_cache) && !string.IsNullOrEmpty(strResult))
            {
                #region Da luu cache
                //strResult = HttpContext.Current.Cache.Get("Treebuild_strResult" + HttpContext.Current.User.Identity.Name) as string;

                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
                #endregion
            }
            else
            {
                #region Doi voi truong hop chua luu Cache
                // Doi voi truong hop chua luu cache
                //thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
                MainSecurity objSec = new MainSecurity();
                Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
                if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap)
                {
                    strResult = LoadAllCat(cb_Cate, null, null);
                }
                else
                {
                    strResult = CafeF_LoadCategoryByPermission(cb_Cate, null, null);
                }
                if (strResult != "")
                    strResult = strResult.Substring(0, strResult.Length - 1);

                str_item_cache = "";
                foreach (ListItem item in cb_Cate.Items)
                {
                    str_item_cache += "!" + item.Text + "|" + item.Value;
                }

                if (str_item_cache.Trim() != "")
                    str_item_cache = str_item_cache.Substring(1);

                HttpContext.Current.Cache.Insert("CafeF_Treebuild_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                HttpContext.Current.Cache.Insert("CafeF_Treebuild_strResult" + HttpContext.Current.User.Identity.Name, strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                #endregion
            }
            return strResult;
        }

		public static string TreebuildAllCat(DropDownList cb_Cate)
		{
			cb_Cate.Items.Clear();
			string strResult = "";

            string str_item_cache = HttpContext.Current.Cache.Get("TreebuildAllCat_" + HttpContext.Current.User.Identity.Name) as string;
            if (!string.IsNullOrEmpty(str_item_cache))
            {
                #region Da Luu Cache

                strResult = HttpContext.Current.Cache.Get("TreebuildAllCat_strResult" + HttpContext.Current.User.Identity.Name) as string;

                // Doi voi truong hop luu Cache
                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
                #endregion
            }
			else
			{
				#region Doi voi truong hop chua luu Cache
				strResult = LoadAllCat(cb_Cate, null, null);

				if (strResult != "")
					strResult = strResult.Substring(0, strResult.Length - 1);

				str_item_cache = "";
				foreach (ListItem item in cb_Cate.Items)
				{
					str_item_cache += "!" + item.Text + "|" + item.Value;
				}

				if (str_item_cache.Trim() != "")
					str_item_cache = str_item_cache.Substring(1);

				HttpContext.Current.Cache.Insert("TreebuildAllCat_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				HttpContext.Current.Cache.Insert("TreebuildAllCat_strResult" + HttpContext.Current.User.Identity.Name, strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				#endregion
			}
			
			return strResult;
		}

        public static string TreebuildAllCat_by_ChannelID(DropDownList cb_Cate, string channel_id)
        {
            cb_Cate.Items.Clear();
            string strResult = "";
            string cacheName = "TreebuildAllCat_by_Channel_" + HttpContext.Current.User.Identity.Name + "_" + channel_id;
            string str_item_cache = HttpContext.Current.Cache.Get(cacheName) as string;
            if (!string.IsNullOrEmpty(str_item_cache))
            {
                #region Da Luu Cache

                strResult = HttpContext.Current.Cache.Get(cacheName + "__strResult") as string;

                // Doi voi truong hop luu Cache
                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
                #endregion
            }
            else
            {
                #region Doi voi truong hop chua luu Cache
                strResult = LoadAllCatbyChannelID(cb_Cate, null, null, channel_id);

                if (strResult != "")
                    strResult = strResult.Substring(0, strResult.Length - 1);

                str_item_cache = "";
                foreach (ListItem item in cb_Cate.Items)
                {
                    str_item_cache += "!" + item.Text + "|" + item.Value;
                }

                if (str_item_cache.Trim() != "")
                    str_item_cache = str_item_cache.Substring(1);

                HttpContext.Current.Cache.Insert(cacheName, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                HttpContext.Current.Cache.Insert(cacheName + "_strResult", strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                #endregion
            }

            return strResult;
        }


		public static string GetCatIDByUser()
		{
			string strResult = HttpContext.Current.Cache.Get("GetCatIDByUser_strResult" + HttpContext.Current.User.Identity.Name) as string;


            if (string.IsNullOrEmpty(strResult))
			{
				#region Doi voi truong hop chua luu Cache
				// Doi voi truong hop chua luu cache
				//thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
				MainSecurity objSec = new MainSecurity();
				Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
				if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap)
				{
					strResult = LoadAllCat(null, null, null);
				}
				else
				{
					strResult = LoadCategoryByPermission(null, null, null);
				}
				if (strResult != "")
					strResult = strResult.Substring(0, strResult.Length - 1);


				HttpContext.Current.Cache.Insert("GetCatIDByUser_strResult" + HttpContext.Current.User.Identity.Name, strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				#endregion
			}
			
			return strResult;
		}

        /* tiennv sua 20-05-2010 : lay category theo permission cua user */

        public static string CafeF_GetCatIDByUser_Permission(string user)
        {            
            string strResult = HttpContext.Current.Cache.Get("CafeF_GetCatIDByUser_Permission_strResult_" + user) as string;


            if (string.IsNullOrEmpty(strResult))
            {
                #region Doi voi truong hop chua luu Cache
                // Doi voi truong hop chua luu cache
                //thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
                MainSecurity objSec = new MainSecurity();
                Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
                if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap || user == "channelvn" || user == "admin")
                {
                    strResult = LoadAllCat(null, null, null);
                }
                else
                {
                    strResult = CafeF_LoadCategoryByPermission(user);
                }
                if (strResult != "")
                    strResult = strResult.Substring(0, strResult.Length - 1);


                HttpContext.Current.Cache.Insert("CafeF_GetCatIDByUser_Permission_strResult_" + user, strResult, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                #endregion
            }

            return strResult;
        }


		public static void BindCheckBoxListCat(CheckBoxList cb_Cate)
		{
			cb_Cate.Items.Clear();
			string strResult = "";

           
            string str_item_cache = HttpContext.Current.Cache.Get("CheckBoxList_" + HttpContext.Current.User.Identity.Name) as string;
            if (!string.IsNullOrEmpty(str_item_cache))
            {
                // Doi voi truong hop da luu Cache
                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }

            }else
			{
				#region Doi voi truong hop chua luu cache
				//thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
				MainSecurity objSec = new MainSecurity();
				Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
				if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap)
				{

					strResult = LoadAllCat(null, null, cb_Cate);
				}
				else
				{
					strResult = LoadCategoryByPermission(null, null, cb_Cate);
				}

				str_item_cache = "";
				foreach (ListItem item in cb_Cate.Items)
				{
					str_item_cache += "!" + item.Text + "|" + item.Value;
				}

				if (str_item_cache.Trim() != "")
					str_item_cache = str_item_cache.Substring(1);

				HttpContext.Current.Cache.Insert("CheckBoxList_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				#endregion
			}
			


		}

		public static void BindListCat(ListBox cb_Cate)
		{
			cb_Cate.Items.Clear();
			string strResult = "";

            string str_item_cache = HttpContext.Current.Cache.Get("BindListCat_" + HttpContext.Current.User.Identity.Name) as string;
            if (!string.IsNullOrEmpty(str_item_cache))
            {
                // Doi voi truong hop da luu Cache
                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
            }
            else
			{
				#region Doi voi truong hop chua luu cache
				//thuc hien lay danh sach cat ma nguoi hien tai duoc phep - Check neu nguoi hien tai khong phai la TBT - TKTS thi moi ap dung
				MainSecurity objSec = new MainSecurity();
				Role objrole = objSec.GetRole(HttpContext.Current.User.Identity.Name);
				if (objrole.isPhuTrachKenh || objrole.isThuKyToaSoan || objrole.isTongBienTap)
				{

					strResult = LoadAllCat(null, cb_Cate, null);
				}
				else
				{
					strResult = LoadCategoryByPermission(null, cb_Cate, null);
				}

				str_item_cache = "";
				foreach (ListItem item in cb_Cate.Items)
				{
					str_item_cache += "!" + item.Text + "|" + item.Value;
				}

				if (str_item_cache.Trim() != "")
					str_item_cache = str_item_cache.Substring(1);

				HttpContext.Current.Cache.Insert("BindListCat_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				#endregion
			}
			


		}
		public static void bindAllCat(DropDownList cb_Cate)
		{
            
            string str_item_cache = HttpContext.Current.Cache.Get("bindAllCat_" + HttpContext.Current.User.Identity.Name) as string;
            if (!string.IsNullOrEmpty(str_item_cache))
            {
                // Doi voi truong hop Luu Cache
                string[] strArItem = str_item_cache.Split('!');
                string[] strTextValue;
                ListItem item = new ListItem();
                foreach (string strItem in strArItem)
                {
                    item = new ListItem();
                    strTextValue = strItem.Split('|');
                    item.Text = strTextValue[0];
                    item.Value = strTextValue[1];
                    cb_Cate.Items.Add(item);
                }
            }
            else
			{
				#region Doi Voi truong hop chua luu cache
				LoadAllCat(cb_Cate, null, null);

				str_item_cache = "";
				foreach (ListItem item in cb_Cate.Items)
				{
					str_item_cache += "!" + item.Text + "|" + item.Value;
				}

				if (str_item_cache.Trim() != "")
					str_item_cache = str_item_cache.Substring(1);

				HttpContext.Current.Cache.Insert("bindAllCat_" + HttpContext.Current.User.Identity.Name, str_item_cache, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
				#endregion
			}
			
		}

        public static void CleanCacheCategory()
        {
            #region Xóa cache lưu Category
            DataTable dtUser;
            using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
            {
                dtUser = objDb.UserCollection.GetAllAsDataTable();
            }
            foreach (DataRow dr in dtUser.Rows)
            {
                HttpContext.Current.Cache.Remove("Treebuild_" + dr["User_ID"]);
                HttpContext.Current.Cache.Remove("Treebuild_strResult" + dr["User_ID"]);

                HttpContext.Current.Cache.Remove("TreebuildAllCat_" + dr["User_ID"]);
                HttpContext.Current.Cache.Remove("TreebuildAllCat_strResult" + dr["User_ID"]);

                HttpContext.Current.Cache.Remove("GetCatIDByUser_strResult" + dr["User_ID"]);

                HttpContext.Current.Cache.Remove("BindListCat_" + dr["User_ID"]);

                HttpContext.Current.Cache.Remove("bindAllCat_" + dr["User_ID"]);

                HttpContext.Current.Cache.Remove("CheckBoxList_" + dr["User_ID"]);

            }

            #endregion
        }



		public static ArrayList getCatInfo(int _catid)
		{
			Portal.Core.DAL.CategoryRow objrow;
			ArrayList objresult = new ArrayList();
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				objrow = objDb.CategoryCollection.GetByPrimaryKey(_catid);
			}
			if (objrow != null)
			{
				objresult.Add(objrow.Cat_Name);
				objresult.Add(objrow.Cat_Description);
				objresult.Add(objrow.Cat_DisplayURL);
				objresult.Add(objrow.Cat_ParentID);
				if (!objrow.IsCat_isColumnNull)
					objresult.Add(objrow.Cat_isColumn);
				if (!objrow.IsCat_isHiddenNull)
					objresult.Add(objrow.Cat_isHidden);
				objresult.Add(objrow.Cat_Icon);
				objresult.Add(objrow.Cat_ViewNum);
                
				//SonPC
				objresult.Add(objrow.EditionType_ID);
                objresult.Add(objrow.Cat_Order);
				//SonPC
			}
			return objresult;
		}

		public static Portal.Core.DAL.CategoryRow getCatInfoAsCategoryRow(int _catid)
		{
			Portal.Core.DAL.CategoryRow objrow;
			ArrayList objresult = new ArrayList();
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				objrow = objDb.CategoryCollection.GetByPrimaryKey(_catid);
			}
			if (objrow != null)
				return objrow;

			objrow = new Portal.Core.DAL.CategoryRow();
			return objrow;
		}


		[DataObjectMethod(DataObjectMethodType.Insert)]
		public static void CreateCat(string _catname, string _catdes, string _caturl, int _catparent, int _catedition, string _caticon, bool _catiscolumn, bool _catishide, int _channel)
		{
            CleanCacheCategory();

			Portal.Core.DAL.CategoryRow objrow = new Portal.Core.DAL.CategoryRow();
			objrow.Cat_Name = _catname;
			objrow.Cat_Description = _catdes;
			objrow.Cat_DisplayURL = _caturl;
			objrow.Cat_ParentID = _catparent;
			objrow.EditionType_ID = _catedition;
			objrow.Cat_Icon = _caticon;
			objrow.Cat_isColumn = _catiscolumn;
			objrow.Cat_isHidden = _catishide;
            objrow.Channel_ID = _channel;
			EditionTypeRow objEditRow = null;
			Portal.Core.DAL.CategoryRow objParentRow = null;
			Portal.Core.DAL.CategoryRow objrow_find_parent = null;

			Portal.Core.DAL.CategoryRow[] objTopRows;
			int intMaxOrder = 0;
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
                /* tiennv dong lai : 19-12-2013
				if (_catparent != 0)
				{
					int cat_id_find_parent = GetCatParent(Convert.ToInt32(_catparent));
					objrow_find_parent = objDb.CategoryCollection.GetByPrimaryKey(cat_id_find_parent);
					_catedition = objrow_find_parent.EditionType_ID;
				}
                */
				//if (_catparent == 0)
				//    objEditRow = objDb.EditionTypeCollection.GetByPrimaryKey(_catedition);
				//else
				//{
				objParentRow = objDb.CategoryCollection.GetByPrimaryKey(_catparent);
				//    objEditRow = objDb.EditionTypeCollection.GetByPrimaryKey(objParentRow.EditionType_ID);
				//}

				objEditRow = objDb.EditionTypeCollection.GetByPrimaryKey(_catedition);

				objTopRows = objDb.CategoryCollection.GetTopAsArray(1, "Cat_ParentID=" + _catparent, "Cat_Order DESC");
				if (objTopRows.Length == 1)
				{
					if (objTopRows[0].IsCat_OrderNull)
						intMaxOrder = 1;
					else
						intMaxOrder = objTopRows[0].Cat_Order + 1;
				}
				objrow.Cat_Order = intMaxOrder;
				objrow.EditionType_ID = _catedition;
				objDb.CategoryCollection.Insert(objrow);
			}
            /* bo cai nay di ko can nua
			#region Cập nhập vào bảng Category ở database User
			Xmobile.Users.CategoryRow objCateUserRow = new Xmobile.Users.CategoryRow();
			objCateUserRow.Cat_ID = objrow.Cat_ID;
			objCateUserRow.Cat_Name = objrow.Cat_Name;
			objCateUserRow.Cat_ParentID = objrow.Cat_ParentID;
			objCateUserRow.Cat_Order = objrow.Cat_Order;
			objCateUserRow.Channel_ID = Portal.API.Config.CurrentChannel;
			using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
			{
				objDb.CategoryCollection.Insert(objCateUserRow);
			}
			#endregion

            */
           

			//cap nhat thong tin ve tab moi khi them cat
			string strEditionRef = "";
			string strParentTabRef = "";
			if (objEditRow != null)
			{
				//strEditionRef = objEditRow.EditionDisplayURL;
				//SonPC
				String UseEditionType = ConfigurationManager.AppSettings["UseEditionType"];
				if (UseEditionType.ToLower().IndexOf("false") >= 0)
					strEditionRef = ConfigurationManager.AppSettings["PrefixEditionType"];
				else
					strEditionRef = objEditRow.EditionDisplayURL;

				//SonPC
			}
			if (objParentRow != null)
			{
				strParentTabRef = objParentRow.Cat_DisplayURL;
			}
			//thuc hien lay doi tuong GenTabHelper
			Portal.BO.Gentabhelper objHelp = new Gentabhelper();
			objHelp.AddCategoryTab(strEditionRef, strParentTabRef, _caturl, _catname);

		}

        public static void UpdateCategory(int _catid, string _catname, string _catdisplayurl, string _caticon, string _catorder,bool isCatHidden)
        {
            
			Portal.Core.DAL.CategoryRow objrow;
			Portal.Core.DAL.EditionTypeRow objEditRow = null;
			Portal.Core.DAL.CategoryRow objParentRow = null;
			Portal.Core.DAL.CategoryRow objrow_find_parent = null;

			string strOldUrl = "";
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				objrow = objDb.CategoryCollection.GetByPrimaryKey(_catid);
				objParentRow = objDb.CategoryCollection.GetByPrimaryKey(objrow.Cat_ParentID);
				objEditRow = objDb.EditionTypeCollection.GetByPrimaryKey(objrow.EditionType_ID);
			}
            if (objrow != null)
            {
                strOldUrl = objrow.Cat_DisplayURL;
                objrow.Cat_Name = _catname;
                objrow.Cat_Description = objrow.Cat_Description;
                objrow.Cat_DisplayURL = _catdisplayurl;
                objrow.Cat_ParentID = objrow.Cat_ParentID;
                objrow.EditionType_ID = objrow.EditionType_ID;
                objrow.Channel_ID = objrow.Channel_ID;
                objrow.Cat_isHidden = isCatHidden;
                //objrow.EditionType_ID = objrow_find_parent.EditionType_ID;
                if (_caticon != null && _caticon != "")
                    objrow.Cat_Icon = _caticon;
                objrow.Cat_isColumn = objrow.Cat_isColumn;
                objrow.Cat_isHidden = objrow.Cat_isHidden;
                objrow.Cat_Order = Convert.ToInt32(_catorder);
                using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
                {
                    objDb.CategoryCollection.Update(objrow);
                    UpdateEditionType(objrow.Cat_ID, objrow.EditionType_ID, objDb);
                }
            }
        }

		[DataObjectMethod(DataObjectMethodType.Insert)]
		public static void UpdateCate(int _catid, string _catname, string _catdes, string _caturl, int _catparent, int _catedition, string _caticon, bool _catiscolumn, bool _catishide, Int32 _channel)
		{

            CleanCacheCategory();

			Portal.Core.DAL.CategoryRow objrow;
			Portal.Core.DAL.EditionTypeRow objEditRow = null;
			Portal.Core.DAL.CategoryRow objParentRow = null;
			Portal.Core.DAL.CategoryRow objrow_find_parent = null;

			string strOldUrl = "";
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				objrow = objDb.CategoryCollection.GetByPrimaryKey(_catid);
				objParentRow = objDb.CategoryCollection.GetByPrimaryKey(objrow.Cat_ParentID);
				objEditRow = objDb.EditionTypeCollection.GetByPrimaryKey(objrow.EditionType_ID);

				if (_catparent != 0)
				{
					int cat_id_find_parent = GetCatParent(Convert.ToInt32(_catparent));
					objrow_find_parent = objDb.CategoryCollection.GetByPrimaryKey(cat_id_find_parent);
					_catedition = objrow_find_parent.EditionType_ID;
				}
			}
			if (objrow != null)
			{
				strOldUrl = objrow.Cat_DisplayURL;
				objrow.Cat_Name = _catname;
				objrow.Cat_Description = _catdes;
				objrow.Cat_DisplayURL = _caturl;
				objrow.Cat_ParentID = _catparent;
				objrow.EditionType_ID = _catedition;
                objrow.Channel_ID = _channel;
				//objrow.EditionType_ID = objrow_find_parent.EditionType_ID;
				if (_caticon != null && _caticon != "")
					objrow.Cat_Icon = _caticon;
				objrow.Cat_isColumn = _catiscolumn;
				objrow.Cat_isHidden = _catishide;
				using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
				{
					objDb.CategoryCollection.Update(objrow);
					UpdateEditionType(objrow.Cat_ID, objrow.EditionType_ID, objDb);
				}
				//cap nhat lai tabref neu co su thay doi ten o displayUrl
				string strEditionRef = "";
				string strParentTabRef = "";
				string strNewRef = "";
				string strOldRef = "";
				if (objEditRow != null)
				{
					//strEditionRef = objEditRow.EditionDisplayURL;
					//SonPC
					String UseEditionType = System.Configuration.ConfigurationSettings.AppSettings["UseEditionType"];
					if (UseEditionType.ToLower().IndexOf("false") >= 0)

						strEditionRef = System.Configuration.ConfigurationSettings.AppSettings["PrefixEditionType"];
					else
						strEditionRef = objEditRow.EditionDisplayURL;
					//SonPC


					strOldRef = strEditionRef + ".";
					strNewRef = strEditionRef + ".";
				}
				if (objParentRow != null)
				{
					strParentTabRef = objParentRow.Cat_DisplayURL;
					strOldRef = strOldRef + "." + strParentTabRef + ".";
					strNewRef = strNewRef + "." + strParentTabRef + ".";
				}
				strOldRef = strOldRef + strOldUrl;
				strNewRef = strNewRef + _caturl;
				Portal.BO.Gentabhelper objHelp = new Gentabhelper();
				objHelp.SyncCategoryTab(strOldRef, strNewRef, _catname, strEditionRef, strParentTabRef, _caturl, _catname);

                /*
				#region Cập nhập vào bảng Category ở database User
				Xmobile.Users.CategoryRow objCateUserRow = new Xmobile.Users.CategoryRow();
				using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
				{
					objCateUserRow = objDb.CategoryCollection.GetByPrimaryKey(objrow.Cat_ID, Portal.API.Config.CurrentChannel);
				}
				objCateUserRow.Cat_Name = objrow.Cat_Name;
				objCateUserRow.Cat_Parent = objrow.Cat_ParentID;
				objCateUserRow.Cat_Order = objrow.Cat_Order;
				objCateUserRow.Channel_ID = Portal.API.Config.CurrentChannel;
				using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
				{
					objDb.CategoryCollection.Update(objCateUserRow);
				}
                
				#endregion
                */



            }
		}


		
		public static string GetChildCatIdByCatParentId(int cat_Parent_Id)
		{
			DataTable dt;
			string strCatId = "";
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				dt = objDb.CategoryCollection.GetAsDataTable(" Cat_ParentID = " + cat_Parent_Id.ToString(), "");
			}

			foreach (DataRow dr in dt.Rows)
			{
				strCatId += "," + dr["Cat_ID"];
			}
			if (strCatId.Trim() != "")
				strCatId = strCatId.Substring(1);

			return strCatId;
		}

		public static string GetTabReferenceByCatId(int catId)
		{
			string tab = string.Empty;

			Portal.Core.DAL.MainDB db = new Portal.Core.DAL.MainDB();
			Portal.Core.DAL.CategoryRow category = db.CategoryCollection.GetByPrimaryKey(catId);
			if (category != null)
			{
				tab = category.Cat_DisplayURL;
				while (category.Cat_ParentID != 0)
				{
					category = db.CategoryCollection.GetByPrimaryKey(category.Cat_ParentID);
					if (category != null) tab = category.Cat_DisplayURL + "." + tab;
				}
				tab = db.EditionTypeCollection.GetByPrimaryKey(category.EditionType_ID).EditionDisplayURL + "." + tab;
			}

			return tab;
		}

		public static bool Delete(int catId)
		{
            CleanCacheCategory();
			using (Portal.Core.DAL.MainDB db = new Portal.Core.DAL.MainDB())
				db.StoredProcedures.Category_Delete(catId);
			using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
				db.StoreProcedure.Category_Delete(catId);
			return true;
		}

        public static DataTable GetCategoryByUserID(string userid)
        {
            DataTable dt = new DataTable();
            using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
            {
                dt = db.StoreProcedure.vc_Execute_Sql("select * from Category where Channel_ID = (Select Distinct Channel_ID from VIEW_GetChannel where User_ID='" + userid + "')");
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        public static DataTable GetCategoryByChannelID(string id)
        {
            DataTable dt = new DataTable();
            using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
            {
                dt = db.StoreProcedure.vc_Execute_Sql("select * from Category where Channel_ID = (Select Channel_ID from Channel where Channel_ID=" + id + ")");
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        public static DataTable GetCategoryByChannelName(string Channel_Name)
        {
            DataTable dt = new DataTable();
            using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
            {
                dt = db.StoreProcedure.vc_Execute_Sql("select * from Category where Channel_ID = (Select Channel_ID from Channel where Channel_Name='"+ Channel_Name +"')");
                db.Close();
                db.Dispose();
            }

            return dt;
        }


        #region Get cat parent
        public static DataTable GetCategoryParentByUserID(string userid)
        {
            DataTable dt = new DataTable();
            using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
            {
                dt = db.StoreProcedure.vc_Execute_Sql("select * from Category where Cat_ParentID = 0 and Channel_ID = (Select Distinct Channel_ID from VIEW_GetChannel where User_ID='" + userid + "')");
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        public static DataTable GetCategoryParentByChannelID(string id)
        {
            DataTable dt = new DataTable();
            using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
            {
                dt = db.StoreProcedure.vc_Execute_Sql("select * from Category where Cat_ParentID = 0 and Channel_ID = (Select Channel_ID from Channel where Channel_ID=" + id + ")");
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        public static DataTable GetCategoryParentByChannelName(string Channel_Name)
        {
            DataTable dt = new DataTable();
            using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
            {
                dt = db.StoreProcedure.vc_Execute_Sql("select * from Category where Cat_ParentID = 0 and Channel_ID = (Select Channel_ID from Channel where Channel_Name='" + Channel_Name + "')");
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        public static DataTable GetCategoriesByParent(int parentId)
        {
            Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB();
            return objDb.CategoryCollection.GetAsDataTable("Cat_ParentID = " + parentId, "Cat_Order");
        }
            
        #endregion



       

		#region bacth [4:07 PM 5/23/2008]
		public static Portal.Core.DAL.CategoryRow CreateCat2(string name, int order, string description, string displayURL, int parentCatId, int editionId, string icon, bool isColumn, bool isHidden, string tabCloneRef)
		{
            CleanCacheCategory();
			#region Cập nhật vào bảng Category ở database dữ liệu chính [Portal.Core.DAL.CategoryRow]
			Portal.Core.DAL.CategoryRow objrow = new Portal.Core.DAL.CategoryRow();
			objrow.Cat_Name = name;
			objrow.Cat_Description = description;
			objrow.Cat_DisplayURL = displayURL;
			objrow.Cat_ParentID = parentCatId;
			objrow.EditionType_ID = editionId;
			objrow.Cat_Icon = icon;
			objrow.Cat_Order = order;
			objrow.Cat_isColumn = isColumn;
			objrow.Cat_isHidden = isHidden;
			Portal.Core.DAL.MainDB db = new Portal.Core.DAL.MainDB();
			db.CategoryCollection.Insert(objrow);

			#endregion

            /*
			#region Cập nhập vào bảng Category ở database User [Xmobile.Users.CategoryRow]
			Xmobile.Users.CategoryRow objCateUserRow = new Xmobile.Users.CategoryRow();
			objCateUserRow.Cat_ID = objrow.Cat_ID;
			objCateUserRow.Cat_Name = objrow.Cat_Name;
			objCateUserRow.Cat_Parent = objrow.Cat_ParentID;
			objCateUserRow.Cat_Order = objrow.Cat_Order;
			objCateUserRow.Channel_ID = Portal.API.Config.CurrentChannel;
			using (Xmobile.Users.MainDB userDB = new Xmobile.Users.MainDB())
			{
				userDB.CategoryCollection.Insert(objCateUserRow);
			}
			#endregion
            */

			#region Cập nhật file Portal.config
			string parentTabRef = string.Empty;
			if (parentCatId == 0)
				parentTabRef = db.EditionTypeCollection.GetByPrimaryKey(editionId).EditionDisplayURL;
			else
				CategoryHelper.GetTabReferenceByCatId(parentCatId);

			string newTabRef = parentTabRef + "." + name;
			Portal.BO.Gentabhelper.AddNewTab(newTabRef, parentTabRef, name, tabCloneRef);
			#endregion

			return objrow;
		}

        public static Portal.Core.DAL.CategoryRow CreateNewCat(string name, int order, string description, string displayURL, int parentCatId, int editionId, string icon, bool isColumn, bool isHidden, string tabCloneRef, int channel_id)
        {
            CleanCacheCategory();
            #region Cập nhật vào bảng Category ở database dữ liệu chính [Portal.Core.DAL.CategoryRow]
            Portal.Core.DAL.CategoryRow objrow = new Portal.Core.DAL.CategoryRow();
            objrow.Cat_Name = name;
            objrow.Cat_Description = description;
            objrow.Cat_DisplayURL = displayURL;
            objrow.Cat_ParentID = parentCatId;
            objrow.EditionType_ID = editionId;
            objrow.Cat_Icon = icon;
            objrow.Cat_Order = order;
            objrow.Cat_isColumn = isColumn;
            objrow.Cat_isHidden = isHidden;
            objrow.Channel_ID = channel_id;
            Portal.Core.DAL.MainDB db = new Portal.Core.DAL.MainDB();
            db.CategoryCollection.Insert(objrow);

            #endregion

            /*
			#region Cập nhập vào bảng Category ở database User [Xmobile.Users.CategoryRow]
			Xmobile.Users.CategoryRow objCateUserRow = new Xmobile.Users.CategoryRow();
			objCateUserRow.Cat_ID = objrow.Cat_ID;
			objCateUserRow.Cat_Name = objrow.Cat_Name;
			objCateUserRow.Cat_Parent = objrow.Cat_ParentID;
			objCateUserRow.Cat_Order = objrow.Cat_Order;
			objCateUserRow.Channel_ID = Portal.API.Config.CurrentChannel;
			using (Xmobile.Users.MainDB userDB = new Xmobile.Users.MainDB())
			{
				userDB.CategoryCollection.Insert(objCateUserRow);
			}
			#endregion
            

            #region Cập nhật file Portal.config
            string parentTabRef = string.Empty;
            if (parentCatId == 0)
                parentTabRef = db.EditionTypeCollection.GetByPrimaryKey(editionId).EditionDisplayURL;
            else
                CategoryHelper.GetTabReferenceByCatId(parentCatId);

            string newTabRef = parentTabRef + "." + name;
            Portal.BO.Gentabhelper.AddNewTab(newTabRef, parentTabRef, name, tabCloneRef);
            #endregion
            */
            return objrow;
        }


		public static bool UpdateCategoryRow(Portal.Core.DAL.CategoryRow row)
		{
            CleanCacheCategory();
            
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				objDb.CategoryCollection.Update(row);
			}

            /* bo di ko can nua
			#region Cập nhập vào bảng Category ở database User
			Xmobile.Users.CategoryRow objCateUserRow = new Xmobile.Users.CategoryRow();
			using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
			{
				objCateUserRow = objDb.CategoryCollection.GetByPrimaryKey(row.Cat_ID, Portal.API.Config.CurrentChannel);
				objCateUserRow.Cat_Name = row.Cat_Name;
				objCateUserRow.Cat_Parent = row.Cat_ParentID;
				objCateUserRow.Cat_Order = row.Cat_Order;
				objCateUserRow.Channel_ID = Portal.API.Config.CurrentChannel;
				objDb.CategoryCollection.Update(objCateUserRow);
			}
			#endregion
            */
			
			return true;
		}
		#endregion

		#region SonPC 13/02/2008
		//SonPC 13/02/2008
		public static int GetCatParent(Int32 Cate_ID)
		{
			Portal.Core.DAL.CategoryRow objrow;
			using (Portal.Core.DAL.MainDB objDb = new Portal.Core.DAL.MainDB())
			{
				objrow = objDb.CategoryCollection.GetByPrimaryKey(Cate_ID);
				if (objrow.Cat_ParentID == 0)
					return Cate_ID;
				else
					return GetCatParent(objrow.Cat_ParentID);
			}
		}

		public static bool CheckUpdateCate(int Cate_ID, int NewsParent, bool IsFirst)
		{
			if (NewsParent == 0)
				return true;
			if (IsFirst && (Cate_ID == NewsParent))
				return false;
			using (Portal.Core.DAL.MainDB db = new Portal.Core.DAL.MainDB())
			{
				Portal.Core.DAL.CategoryRow objrow;
				objrow = db.CategoryCollection.GetByPrimaryKey(NewsParent);
				if (Cate_ID == objrow.Cat_ParentID)
					return false;
				else return CheckUpdateCate(Cate_ID, objrow.Cat_ParentID, false);
			}
		}
		//SonPC 13/02/2008

		public static void UpdateEditionType(Int32 Cat_ID, Int32 EditionType, Portal.Core.DAL.MainDB db)
		{
			DataTable table = db.CategoryCollection.GetAsDataTable(" Cat_ParentID = " + Cat_ID.ToString(), "");
			if (table.Rows.Count != 0)
			{
				int RowCount = table.Rows.Count;
				for (int i = 0; i < RowCount; i++)
				{
					//Update
					Int32 Cat_ID_Child = Convert.ToInt32(table.Rows[i]["Cat_ID"]);
					db.StoredProcedures.vc_Category_Update_EditionType(Cat_ID_Child, EditionType);

					//De quy

					UpdateEditionType(Cat_ID_Child, EditionType, db);
					//De quy
				}
			}
		}
		#endregion


		public static DataTable GetListCategory(string strNotInCatId)
		{
			strNotInCatId = "-1";
			if (strNotInCatId.Trim() == "")
				strNotInCatId = "-1";

			DataTable dtCat = new DataTable();
			DataTable objresult = new DataTable();
			DataTable dtz = new DataTable();
			DataTable dt3 = new DataTable();
			object[] objarr = null;
			Portal.Core.DAL.MainDB objDbCore = new Portal.Core.DAL.MainDB();
			try
			{
				dtCat = objDbCore.FrontEndStoreProcedure.Category_GetListByWhere(" Where (Cat_ParentID = 0 or Cat_ParentID is null) AND Cat_ID NOT IN (" + strNotInCatId + ") ", "");
				dtCat.Columns.Add(new DataColumn("ID"));
				dtCat.Columns.Add(new DataColumn("Title"));
				objresult = dtCat.Clone();

				foreach (DataRow objrow in dtCat.Rows)
				{
					objarr = objrow.ItemArray;
					objarr.SetValue(objrow["Cat_ID"], objarr.Length - 2);
					objarr.SetValue(objrow["Cat_Name"], objarr.Length - 1);
					objresult.Rows.Add(objarr);

					// Get cac chuyen muc con
					dtz = objDbCore.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID = " + objrow["Cat_ID"] + " AND Cat_ID NOT IN (" + strNotInCatId + ") ", "");
					dtz.Columns.Add(new DataColumn("ID"));
					dtz.Columns.Add(new DataColumn("Title"));

					foreach (DataRow drz in dtz.Rows)
					{
						drz["Cat_Name"] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + drz["Cat_Name"];
						objarr = drz.ItemArray;
						objarr.SetValue(drz["Cat_ID"], objarr.Length - 2);
						objarr.SetValue(drz["Cat_Name"], objarr.Length - 1);
						objresult.Rows.Add(objarr);

						#region Chuyen muc con cap 3
						// Get cac chuyen muc con cap 3
						dt3 = objDbCore.FrontEndStoreProcedure.Category_GetListByWhere(" Where Cat_ParentID = " + drz["Cat_ID"] + " AND Cat_ID NOT IN (" + strNotInCatId + ") ", "");
						dt3.Columns.Add(new DataColumn("ID"));
						dt3.Columns.Add(new DataColumn("Title"));

						if (dt3.Rows.Count > 0)
						{
							foreach (DataRow dr3 in dt3.Rows)
							{
								dr3["Cat_Name"] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dr3["Cat_Name"];
								objarr = dr3.ItemArray;
								objarr.SetValue(dr3["Cat_ID"], objarr.Length - 2);
								objarr.SetValue(dr3["Cat_Name"], objarr.Length - 1);
								objresult.Rows.Add(objarr);
							}
						}
						#endregion


					}
				}
			}

			catch (Exception e)
			{
			}

			dtCat.Dispose();
			dtz.Dispose();
			return objresult;
		}


        public static void CleanCachePermission()
        {
            #region Permission
            DataTable dtUser;
            using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
            {
                dtUser = objDb.UserCollection.GetAllAsDataTable();
            }
            foreach (DataRow dr in dtUser.Rows)
            {
                HttpContext.Current.Cache.Remove("GetPermission1_" + dr["User_ID"]);

            }

            #endregion
        }
	}
}
